<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" :inline="true" size="small">
            <el-form-item label="批次号">
              <el-input v-model="queryData.name" :rows="1" style="width: 280px;" type="textarea" placeholder="多个单号用回车分割" />
            </el-form-item>
            <el-form-item label="客户">
              <el-select v-model="queryData.orgOwner" clearable filterable>
                <el-option v-for="(d, i) in customerOptions" :key="i" :label="d.orgOwnerName" :value="d.orgOwner" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchData()">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;display: flex;">
          <el-input v-model="trackingNumber" style="width: 280px;" placeholder="原单号" @keyup.enter.native="print" />
          <el-button size="small" @click="print">换单</el-button>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe tooltip-effect="dark" :max-height="tableHeight" border>
          <el-table-column prop="name" fixed label="批次号" />
          <el-table-column prop="orgOwnerName" label="客户" />
          <el-table-column prop="totalCarton" label="总件数" />
          <el-table-column prop="totalCartonPrepared" label="有面单件数" />
          <el-table-column prop="totalCartonReplaced" label="已换单件数" />
          <el-table-column prop="createdAt" label="创建时间" />
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button size="small" shape="circle" type="text" @click="downloadLabel(scope.row)">下载上传的面单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <!-- 分页 -->
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50, 100, 200, 300, 400]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="float: right;" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import request from '@/utils/request'
export default {
  data() {
  return {
      trackingNumber: '',
      formData: {
        name: ''
      },
      queryData: {
        pageSize: 50,
        currentPage: 0,
        name: '',
        orgOwner: ''
      },
      tableData: [],
      currentPage: 1, // 分页
      total: 0,
      labelIds: []
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 250
    },
    ...mapState(//
      {
        customerOptions: state => state.base.customerOptions
      }
    )
  },
  created() {
    this.queryOrder()
  },
  methods: {
    print(event) {
      if (event.key !== 'Enter' || !this.trackingNumber) {
        return
      }
      const tn = this.trackingNumber
      this.trackingNumber = ''
      request({
        url: '/replacelabel/replace',
        method: 'post',
        data: { trackingNumber: tn }
      }).then(res => {
        if (res.success) {
          request({
            url: res.data,
            method: 'get'
          })
        }
      })
    },
    searchData() {
      this.queryData.currentPage = 1
      this.queryOrder()
    },
    // 搜索
    queryOrder() {
      request({
        url: 'replacelabel/query',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('是否取消的订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

      })
    },
    downloadLabel(row) {
      request({
        url: '/replacelabel/download?id=' + row.id,
        method: 'get'
      }).then(res => {
        if (res.success) {
          res.data.forEach(e => {
            window.open(e)
          })
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.queryData.currentPage = 1
      this.queryData.pageSize = val
      this.queryOrder()
    },
    handleCurrentChange(val) {
      this.queryData.currentPage = val
      this.queryOrder()
    }
  }
}
</script>
